/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-07-18
 * Time: 21:17
 */
import com.mysql.jdbc.jdbc2.optional.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;


public class JDBCDemo1 {
    public static void main(String[] args) throws SQLException {
        //假定数据库里面有一个 student 表(id name),往里面插入一些数据

        //让用户自行输入学号和姓名
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的学号: ");
        int id = scanner.nextInt();
        System.out.println("请输入你的姓名: ");
        String name = scanner.next();





        //1.创建"数据源"
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/yzy1?characterEncoding=utf8&useSSL=false");
        //设置用户
        ((MysqlDataSource) dataSource).setUser("root");
        //设置密码
        ((MysqlDataSource) dataSource).setPassword("yzy824ap0stol");


        //2.跟数据库服务器建立连接
        Connection connection= dataSource.getConnection();


        //3.SQL语句构造
        //String sql = "insert into student values("+ id +",' "+ name +" ')";
        String sql = "insert into student values(?,?)";
        PreparedStatement statement= connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);


        //4.执行sql语句
        int n=statement.executeUpdate(); //返回值为这次操作影响了几行

        System.out.println("一共影响了: "+n+"行");


        //5.释放必要资源
        statement.close();
        connection.close();
    }
}
